﻿Public Class ClsLogFile
    Inherits ClsFile
    'represents Log file

#Region "Attributes"
#Region "Private attributes"
    Private MoFile As IO.StreamWriter 'used for writing to file
    Private MbLogSwitch As Boolean = True 'switch-logging is on or off
#End Region
#End Region
#Region "Constructors"
    Sub New()
        MyBase.New()
    End Sub

    Sub New(ByVal IsPath As String)
        MyBase.New(IsPath)
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public Property LogSwitch() As Boolean
        Get
            Return MbLogSwitch
        End Get
        Set(ByVal IbValue As Boolean)
            MbLogSwitch = IbValue
        End Set
    End Property
#End Region
#End Region
#Region "Functions"
#Region "Open file, Save file, Close file"
    'opens file
    Public Overrides Function OpenFile() As GeActionResult
        Try
            If MyBase.OpenFile = GeActionResult.kkOk Then
                Try
                    ' Create an instance of StreamWriter to append text to a file, creating of a new file
                    MoFile = New IO.StreamWriter(Path, True)

                    MeStateOfFile = GeFileState.kkOpened
                    Return GeActionResult.kkOk
                Catch ex As Exception
                    Throw New Exception
                End Try
            Else
                Debug.Print("CHYBA!!! - ClsLogFile.OpenFile")
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'saves string to file
    Public Function SaveStringToFile(ByVal IsString As String) As GeActionResult
        Try
            If MeStateOfFile = GeFileState.kkOpened Then
                Try
                    MoFile.WriteLine(IsString)
                    MoFile.Flush()

                    Return GeActionResult.kkOk
                Catch ex As Exception
                    Throw New Exception
                End Try
            Else
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
    'closes file
    Public Overrides Function CloseFile() As GeActionResult
        Try
            If MyBase.CloseFile = GeActionResult.kkOk Then
                MoFile.Flush()
                MoFile.Close()

                MeStateOfFile = GeFileState.kkNotInit
                MsPath = ""
                Return GeActionResult.kkOk
            Else
                Debug.Print("CHYBA!!! - CloseFile.CloseFile")
                Throw New Exception
            End If
        Catch ex As Exception
            LogEventFromException(ex, , , )
            Return GeActionResult.kkError
        End Try
    End Function
#End Region
#End Region

End Class